package com.dianping.cat.consumer.heartbeat.model.transform;

import com.dianping.cat.consumer.heartbeat.model.IEntity;
import com.dianping.cat.consumer.heartbeat.model.entity.Detail;
import com.dianping.cat.consumer.heartbeat.model.entity.Disk;
import com.dianping.cat.consumer.heartbeat.model.entity.Extension;
import com.dianping.cat.consumer.heartbeat.model.entity.HeartbeatReport;
import com.dianping.cat.consumer.heartbeat.model.entity.Machine;
import com.dianping.cat.consumer.heartbeat.model.entity.Period;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;











public class DefaultSaxParser
  extends DefaultHandler
{
/*  41 */   private DefaultLinker m_linker = new DefaultLinker(true);

/*  43 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  45 */   private Stack<String> m_tags = new Stack();

/*  47 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  51 */   private StringBuilder m_text = new StringBuilder();


/*  54 */   public static HeartbeatReport parse(InputStream in) throws SAXException, IOException { return (HeartbeatReport)parseEntity(HeartbeatReport.class, new InputSource((InputStream)removeBOM(in))); }



/*  58 */   public static HeartbeatReport parse(Reader reader) throws SAXException, IOException { return (HeartbeatReport)parseEntity(HeartbeatReport.class, new InputSource((Reader)removeBOM(reader))); }



/*  62 */   public static HeartbeatReport parse(String xml) throws SAXException, IOException { return (HeartbeatReport)parseEntity(HeartbeatReport.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  68 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  69 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  71 */       factory.setValidating(false);
/*  72 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  74 */       factory.newSAXParser().parse(is, handler);
/*  75 */       return (T)handler.getEntity();
/*  76 */     } catch (ParserConfigurationException e) {
/*  77 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  82 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  86 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  92 */     if (obj instanceof String) {
/*  93 */       String str = (String)obj;

/*  95 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  96 */         return (T)str.substring(1);
      }
/*  98 */       return obj;
    }
/* 100 */     if (obj instanceof InputStream) {
/* 101 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/* 103 */       in.mark(3);

/* 105 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 106 */         in.reset();
      }

/* 109 */       return (T)in;
/* 110 */     }  if (obj instanceof Reader) {
/* 111 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 113 */       in.mark(1);

/* 115 */       if (in.read() != 65279) {
/* 116 */         in.reset();
      }

/* 119 */       return (T)in;
    }
/* 121 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 127 */     if (value == null || value.length() == 0) {
/* 128 */       return defaultValue;
    }

/* 131 */     if (type == Boolean.class)
/* 132 */       return (T)Boolean.valueOf(value); 
/* 133 */     if (type == Integer.class)
/* 134 */       return (T)Integer.valueOf(value); 
/* 135 */     if (type == Long.class)
/* 136 */       return (T)Long.valueOf(value); 
/* 137 */     if (type == Short.class)
/* 138 */       return (T)Short.valueOf(value); 
/* 139 */     if (type == Float.class)
/* 140 */       return (T)Float.valueOf(value); 
/* 141 */     if (type == Double.class)
/* 142 */       return (T)Double.valueOf(value); 
/* 143 */     if (type == Byte.class)
/* 144 */       return (T)Byte.valueOf(value); 
/* 145 */     if (type == Character.class) {
/* 146 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 148 */     return (T)value;
  }




/* 154 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 159 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 164 */     if (uri == null || uri.length() == 0) {
/* 165 */       Object currentObj = this.m_objs.pop();
/* 166 */       String currentTag = (String)this.m_tags.pop();

/* 168 */       if (currentObj instanceof HeartbeatReport) {
/* 169 */         HeartbeatReport heartbeatReport = (HeartbeatReport)currentObj;

/* 171 */         if ("domain".equals(currentTag)) {
/* 172 */           heartbeatReport.addDomain(getText());
/* 173 */         } else if ("ip".equals(currentTag)) {
/* 174 */           heartbeatReport.addIp(getText());
        }
      }
    }

/* 179 */     this.m_text.setLength(0);
  }


/* 183 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 187 */   protected String getText() { return this.m_text.toString(); }


  private void parseForDetail(Detail parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 191 */     this.m_objs.push(parentObj);
/* 192 */     this.m_tags.push(qName);
  }

  private void parseForDisk(Disk parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 196 */     this.m_objs.push(parentObj);
/* 197 */     this.m_tags.push(qName);
  }

  private void parseForExtension(Extension parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 201 */     if ("detail".equals(qName)) {
/* 202 */       Detail detail = this.m_maker.buildDetail(attributes);

/* 204 */       this.m_linker.onDetail(parentObj, detail);
/* 205 */       this.m_objs.push(detail);
    } else {
/* 207 */       throw new SAXException(String.format("Element(%s) is not expected under extension!", new Object[] { qName }));
    }

/* 210 */     this.m_tags.push(qName);
  }

  private void parseForHeartbeatReport(HeartbeatReport parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 214 */     if ("domain-names".equals(qName) || "domain".equals(qName) || "ips".equals(qName) || "ip".equals(qName)) {
/* 215 */       this.m_objs.push(parentObj);
/* 216 */     } else if ("machine".equals(qName)) {
/* 217 */       Machine machine = this.m_maker.buildMachine(attributes);

/* 219 */       this.m_linker.onMachine(parentObj, machine);
/* 220 */       this.m_objs.push(machine);
    } else {
/* 222 */       throw new SAXException(String.format("Element(%s) is not expected under heartbeat-report!", new Object[] { qName }));
    }

/* 225 */     this.m_tags.push(qName);
  }

  private void parseForMachine(Machine parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 229 */     if ("period".equals(qName)) {
/* 230 */       Period period = this.m_maker.buildPeriod(attributes);

/* 232 */       this.m_linker.onPeriod(parentObj, period);
/* 233 */       this.m_objs.push(period);
    } else {
/* 235 */       throw new SAXException(String.format("Element(%s) is not expected under machine!", new Object[] { qName }));
    }

/* 238 */     this.m_tags.push(qName);
  }

  private void parseForPeriod(Period parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 242 */     if ("disk".equals(qName)) {
/* 243 */       Disk disk = this.m_maker.buildDisk(attributes);

/* 245 */       this.m_linker.onDisk(parentObj, disk);
/* 246 */       this.m_objs.push(disk);
/* 247 */     } else if ("extension".equals(qName)) {
/* 248 */       Extension extension = this.m_maker.buildExtension(attributes);

/* 250 */       this.m_linker.onExtension(parentObj, extension);
/* 251 */       this.m_objs.push(extension);
    } else {
/* 253 */       throw new SAXException(String.format("Element(%s) is not expected under period!", new Object[] { qName }));
    }

/* 256 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 260 */     if ("heartbeat-report".equals(qName)) {
/* 261 */       HeartbeatReport heartbeatReport = this.m_maker.buildHeartbeatReport(attributes);

/* 263 */       this.m_entity = heartbeatReport;
/* 264 */       this.m_objs.push(heartbeatReport);
/* 265 */       this.m_tags.push(qName);
/* 266 */     } else if ("machine".equals(qName)) {
/* 267 */       Machine machine = this.m_maker.buildMachine(attributes);

/* 269 */       this.m_entity = machine;
/* 270 */       this.m_objs.push(machine);
/* 271 */       this.m_tags.push(qName);
/* 272 */     } else if ("period".equals(qName)) {
/* 273 */       Period period = this.m_maker.buildPeriod(attributes);

/* 275 */       this.m_entity = period;
/* 276 */       this.m_objs.push(period);
/* 277 */       this.m_tags.push(qName);
/* 278 */     } else if ("disk".equals(qName)) {
/* 279 */       Disk disk = this.m_maker.buildDisk(attributes);

/* 281 */       this.m_entity = disk;
/* 282 */       this.m_objs.push(disk);
/* 283 */       this.m_tags.push(qName);
/* 284 */     } else if ("extension".equals(qName)) {
/* 285 */       Extension extension = this.m_maker.buildExtension(attributes);

/* 287 */       this.m_entity = extension;
/* 288 */       this.m_objs.push(extension);
/* 289 */       this.m_tags.push(qName);
/* 290 */     } else if ("detail".equals(qName)) {
/* 291 */       Detail detail = this.m_maker.buildDetail(attributes);

/* 293 */       this.m_entity = detail;
/* 294 */       this.m_objs.push(detail);
/* 295 */       this.m_tags.push(qName);
    } else {
/* 297 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 303 */     if (uri == null || uri.length() == 0) {
/* 304 */       if (this.m_objs.isEmpty()) {
/* 305 */         parseRoot(qName, attributes);
      } else {
/* 307 */         Object parent = this.m_objs.peek();
/* 308 */         String tag = (String)this.m_tags.peek();

/* 310 */         if (parent instanceof HeartbeatReport) {
/* 311 */           parseForHeartbeatReport((HeartbeatReport)parent, tag, qName, attributes);
/* 312 */         } else if (parent instanceof Machine) {
/* 313 */           parseForMachine((Machine)parent, tag, qName, attributes);
/* 314 */         } else if (parent instanceof Period) {
/* 315 */           parseForPeriod((Period)parent, tag, qName, attributes);
/* 316 */         } else if (parent instanceof Disk) {
/* 317 */           parseForDisk((Disk)parent, tag, qName, attributes);
/* 318 */         } else if (parent instanceof Extension) {
/* 319 */           parseForExtension((Extension)parent, tag, qName, attributes);
/* 320 */         } else if (parent instanceof Detail) {
/* 321 */           parseForDetail((Detail)parent, tag, qName, attributes);
        } else {
/* 323 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 327 */       this.m_text.setLength(0);
    } else {
/* 329 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }

  protected Date toDate(String str, String format) {
    try {
/* 335 */       return (new SimpleDateFormat(format)).parse(str);
/* 336 */     } catch (ParseException e) {
/* 337 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }

  protected Number toNumber(String str, String format) {
    try {
/* 343 */       return (new DecimalFormat(format)).parse(str);
/* 344 */     } catch (ParseException e) {
/* 345 */       throw new RuntimeException(String.format("Unable to parse number(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/heartbeat/model/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */